package com.pybox.api;

import com.pybox.aop.Mylog;
import com.pybox.dto.*;
import com.pybox.oauth.conf.UserDto;
import com.pybox.service.TUsersService;

import com.pybox.util.UserUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 描述：TUsers Api
 *
 * @author wuyingkai
 * @date 2018-09-15
 */

@Slf4j
@RequestMapping("/TUsers")
@Api(tags = "TUsers管理接口", description = "TUsers管理接口")
@RestController
public class TUsersApi {

    @Autowired
    private TUsersService service;

    @ApiOperation(value = "创建TUsers", notes = "创建TUsers")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @Mylog(value = "新增了人员信息")
    public Result<Boolean> save(@RequestBody TUsersDto dto) {
        service.create(dto);
        return Result.ok(true);
    }

    @ApiOperation(value = "删除TUsers", notes = "删除TUsers")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    @Mylog(value = "删除了人员信息")
    @ApiImplicitParam(name = "id", value = "TUsers id", dataType = "Integer", paramType = "path")
    public Result<Boolean> delete(@PathVariable("id") Integer id) {
        service.delete(id);
        return Result.ok(true);
    }


    @ApiOperation(value = "更新TUsers", notes = "更新TUsers")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.PUT)
    @Mylog(value = "更新了人员信息")
    @ApiImplicitParam(name = "id", value = "TUsers id", dataType = "Integer", paramType = "path")
    public Result<Boolean> update(@PathVariable("id") Integer id, @RequestBody TUsersDto dto) {
        service.update(id, dto);
        return Result.ok(true);
    }

    @ApiOperation(value = "查询TUsers", notes = "查询TUsers")
    @RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
    @ApiImplicitParam(name = "id", value = "TUsers id", dataType = "Integer", paramType = "path")
    public Result<TUsersDto> detail(@PathVariable("id") Integer id) {

        return Result.ok(service.detail(id));
    }

    @ApiOperation(value = "通过登录人员查询TUsers", notes = "查询当前用户信息（包括权限）")
    @RequestMapping(value = "/getLoginUserByUserName/{username}", method = RequestMethod.GET)
    @ApiImplicitParam(name = "username", value = "TUsers username", dataType = "String", paramType = "path")
    public Result<UserDto> getLoginUser(@PathVariable("username") String username) {
        return Result.ok(service.getLoginUser(username));
    }

    @ApiOperation(value = "分页查询TUsers", notes = "分页查询TUsers")
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "groupId", value = "groupId", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "customerId", value = "customerId", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "username", value = "username", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "realname", value = "realname", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "comments", value = "comments", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "deptId", value = "deptId", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "isAdmin", value = "isAdmin", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "pageNo", value = "pageNo", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "pageSize", dataType = "int", paramType = "query")
    })
    public Result<Page<TUsersDto>> page(
            @RequestParam(required = false) String groupId,
            @RequestParam(required = false) String customerId,
            @RequestParam(required = false) String username,
            @RequestParam(required = false) String realname,
            @RequestParam(required = false) String comments,
            @RequestParam(required = false) String deptId,
            @RequestParam(required = false) String isAdmin,
            @RequestParam Integer pageNo,
            @RequestParam Integer pageSize) {

        Map<String, String> params = new HashMap<>();

        params.put("customerId", customerId);
        params.put("groupId", groupId);
        params.put("username", username);
        params.put("realname", realname);
        params.put("comments", comments);
        params.put("deptId", deptId);
        params.put("isAdmin", isAdmin);

        return Result.ok(service.page(pageNo, pageSize, params));
    }

    @ApiOperation(value = "重置用户密码", notes = "重置用户密码")
    @Mylog(value = "重置用户密码")
    @RequestMapping(value = "/resetPassword", method = RequestMethod.POST)
    public Result<Boolean> resetPassword(@RequestBody PasswordDto passwordDto) {
        return service.resetPassword(passwordDto);
    }

    @ApiOperation(value = "设置跟用户关联的分组", notes = "设置跟用户关联的分组")
    @RequestMapping(value = "/updateGroupIdsByUserId", method = RequestMethod.PUT)
    public Result<Boolean> updateGroupIdsByUserId(@RequestBody UserGroupsDto dto) {
        return service.updateGroupIdsByUserId(dto);

    }

    @ApiOperation(value = "设置用户关联的物品", notes = "设置用户关联的物品")
    @RequestMapping(value = "/updateObjectsByUserId", method = RequestMethod.PUT)
    public Result<Boolean> updateObjectsByUserId(@RequestBody UserObjectsDto dto) {
        return service.updateObjectsByUserId(dto);
    }

    @ApiOperation(value = "获取用户关联的分组", notes = "获取用户关联的分组")
    @RequestMapping(value = "/getGroupIdsByUserId/{userId}", method = RequestMethod.GET)
    public Result<UserGroupsDto> getGroupIdsByUserId(@PathVariable Integer userId) {
        return service.getGroupIdsByUserId(userId);
    }

    @ApiOperation(value = "获取用户关联的组id", notes = "获取用户关联的组id")
    @RequestMapping(value = "/getObjectsByUserId/{userId}", method = RequestMethod.GET)
    public Result<UserObjectsDto> getObjectsByUserId(@PathVariable Integer userId) {
        return service.getObjectIdsByUserId(userId);
    }

    @ApiOperation(value = "设置管理员", notes = "设置管理员(0默认管理员，1管理员 2普通（删除管理员即为设置普通）)")
    @RequestMapping(value = "/upDateAdminByUserId/{userId}/{isAdmin}", method = RequestMethod.GET)
    public Result<Boolean> upDateAdminByUserId(@PathVariable Integer userId, @PathVariable Integer isAdmin) {
        return service.upDateAdminByUserId(isAdmin, userId);
    }

    @ApiOperation(value = "新增用户名校验", notes = "新增用户名校验")
    @RequestMapping(value = "/checkUserNameUnique/{username}", method = RequestMethod.GET)
    @ApiImplicitParam(name = "username", value = "TUsers username", dataType = "String", paramType = "path")
    public Result<Boolean> checkUserNameUnique(@PathVariable("username") String username) {
        return service.checkUserNameUnique(username);
    }

    @ApiOperation(value = "用户密码校验", notes = "用户密码校验")
    @RequestMapping(value = "/checkUserPasswordUnique/{password}", method = RequestMethod.GET)
    @ApiImplicitParam(name = "password", value = "TUsers password", dataType = "String", paramType = "path")
    public Result<Boolean> checkUserPasswordUnique(@PathVariable("password") String password) {
        return service.checkUserNameUnique(password);
    }

    @ApiOperation(value = "查询当前客户下所有TUsers(is_admin=0和1的用户)", notes = "查询当前客户下所有超级TUsers")
    @RequestMapping(value = "/getAllShPeople", method = RequestMethod.GET)
    public Result<List<TUsersDto>>  getAllShPeople(){
       return service.getAllShPeople();
    }
}